﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MedicService.Domain;
using MedicService.Domain.Generic;

namespace MedicService.Server.Factory {
    public class DeletedFactory {
        public static void Delete(String clazz, int id){
            string q = "INSERT INTO [Deleted] ([Class],[Id],[Date]) VALUES ('" + clazz + "'," + id + ", "+DateTime(System.DateTime.Now)+"); SELECT 0"; 
            DatabaseConnector.Insert(q);
        }

        public static List<Deleted> getList() {
            var table = DatabaseConnector.GetData("Deleted", "Date");
            var rows = table.Rows;
            var list = new List<Deleted>();
            foreach (DataRow row in rows) {
                var deleted = new Deleted();
                deleted.Id = (int)row["Id"];
                deleted.Class = (string) row["Class"];
            }
            return list;
        }

        public static List<int> getByType<T>() where T : Entity {
            var type = typeof(T);
            var name = type.Name;
            return getByType(name);
        }

        public static List<int> getByType(string type) {

            var table = DatabaseConnector.GetData("Deleted", "Date", "Class='" + type + "'");
            var rows = table.Rows;
            var list = new List<int>();
            foreach (DataRow row in rows) {
                int id = (int) row["Id"];
                list.Add(id);
            }
            return list;
        }
        protected static string DateTime(DateTime dateTime) {
            return "'" + dateTime.Year + X2(dateTime.Month) + X2(dateTime.Day) +
                   " " + X2(dateTime.Hour) + ":" + X2(dateTime.Minute) + ":" + X2(dateTime.Second) + ".000'";
        }
        private static string X2(int i) {
            if (0 <= i && i < 10) {
                return "0" + i;
            } else {
                return "" + i;
            }
        }
    }
}